package com.zhongxi.guoyuan.view.coinShopping;

import android.content.Intent;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;

import com.google.gson.Gson;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshListView;
import com.zhongxi.guoyuan.R;
import com.zhongxi.guoyuan.adapter.coinShop.TodayNewGoodsAdapter;
import com.zhongxi.guoyuan.application.MyApplication;
import com.zhongxi.guoyuan.base.BaseActivity;
import com.zhongxi.guoyuan.entity.BaseResponse;
import com.zhongxi.guoyuan.entity.coinStore.HotGoodsData;
import com.zhongxi.guoyuan.entity.coinStore.HotGoodsResult;
import com.zhongxi.guoyuan.url.Url;

import java.util.ArrayList;
import java.util.List;

public class NewGoodsActivity extends BaseActivity implements View.OnClickListener, PullToRefreshBase.OnRefreshListener2<ListView> {

    private PullToRefreshListView lvList;
//    private Spinner spinner;

    private List<HotGoodsData> data = new ArrayList<>();
    private String type;
    private TodayNewGoodsAdapter adapter;

    @Override
    protected void initView() {
        super.initView();
        setContentView(R.layout.activity_new_goods);
        initTitle();
        tvTitle.setText("银币消费商家");
        lvList = (PullToRefreshListView) findViewById(R.id.lvList);

        //智能排序
//        order();
    }

   /* private void order() {
        //智能排序
        spinner = (Spinner) findViewById(R.id.spinner);
        ArrayAdapter<String> spAdapter = new ArrayAdapter<String>(context, R.layout.simple_spinner_item);
        List<String> spList = new ArrayList<String>();
        spList.add("全部");
        spList.add("美币项目");
        spList.add("返银商品");
        spList.add("银币商品");
        spList.add("银币商家");
        spAdapter.addAll(spList);
        spAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        spinner.setAdapter(spAdapter);
    }*/

    @Override
    protected void initData() {
        super.initData();
        adapter = new TodayNewGoodsAdapter(context, data);
        lvList.setAdapter(adapter);
        //获取今日新品
        getNewGoods();
    }

    private void getNewGoods() {
        type = "newGoods";
        getServer(Url.NEW_GOODS, null, "", false, null, "get");
    }

    @Override
    protected void initEvent() {
        super.initEvent();
        ivBack.setOnClickListener(this);
        lvList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Intent intent = new Intent(context, CoinGoodsActivity.class);
                intent.putExtra("goodsId",data.get(position-1).getId());
                startActivity(intent);
            }
        });
        lvList.setOnRefreshListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.ivBack:
                finish();
                break;
        }
    }

    @Override
    protected void processSuccessResult(String response) {
        super.processSuccessResult(response);
        Gson gson = new Gson();
        try{
            if ("newGoods".equals(type)) {
                HotGoodsResult hotResult = gson.fromJson(response, HotGoodsResult.class);
                MyApplication.getInstance().setSpValue(hotResult.getSafeobj());
                if(pageNum == 1){
                    data.clear();
                }
                if(hotResult.getData().size()>0){
                    data.addAll(hotResult.getData());
                }
                adapter.notifyDataSetChanged();
                lvList.onRefreshComplete();
            }
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    @Override
    protected void ProcessFailResult(String response) {
        super.ProcessFailResult(response);
        Gson gson = new Gson();
        BaseResponse result = gson.fromJson(response, BaseResponse.class);
        if(null!=result.getSafeobj()&&!"".equals(result.getSafeobj())){
            MyApplication.getInstance().setSpValue(result.getSafeobj());
        }
    }

    @Override
    public void onPullDownToRefresh(PullToRefreshBase<ListView> refreshView) {
        pageNum = 1;
        getNewGoods();
    }

    @Override
    public void onPullUpToRefresh(PullToRefreshBase<ListView> refreshView) {
//        pageNum ++;
//        getNewGoods();
    }
}
